<script setup>
import { ref, reactive, onMounted } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'

// 搜索表单
const searchForm = reactive({
  name: '',
  status: ''
})

// 活动列表数据
const activityList = ref([])
const loading = ref(false)

// 分页配置
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0
})

// 表格列配置
const columns = [
  { colKey: 'id', title: 'ID', width: 80 },
  { colKey: 'name', title: '活动名称' },
  { colKey: 'type', title: '活动类型' },
  { colKey: 'startTime', title: '开始时间' },
  { colKey: 'endTime', title: '结束时间' },
  { colKey: 'participants', title: '报名人数' },
  { 
    colKey: 'status', 
    title: '状态',
    cell: (h, { row }) => {
      const statusMap = {
        'pending': { theme: 'warning', text: '未开始' },
        'ongoing': { theme: 'success', text: '进行中' },
        'ended': { theme: 'default', text: '已结束' }
      }
      const status = statusMap[row.status] || statusMap['pending']
      return h('t-tag', {
        theme: status.theme,
        variant: 'light'
      }, status.text)
    }
  },
  { colKey: 'operation', title: '操作', width: 200 }
]

// 获取活动列表
const fetchActivityList = async () => {
  loading.value = true
  try {
    // 模拟数据
    const mockData = Array.from({ length: 42 }, (_, i) => ({
      id: i + 1,
      name: `活动${i + 1}`,
      type: ['文化活动', '户外运动', '美食节', '音乐会'][Math.floor(Math.random() * 4)],
      startTime: new Date(Date.now() + Math.random() * 30 * 24 * 60 * 60 * 1000).toLocaleDateString(),
      endTime: new Date(Date.now() + Math.random() * 60 * 24 * 60 * 60 * 1000).toLocaleDateString(),
      participants: Math.floor(Math.random() * 500) + 50,
      status: ['pending', 'ongoing', 'ended'][Math.floor(Math.random() * 3)]
    }))
    
    // 模拟分页
    const start = (pagination.current - 1) * pagination.pageSize
    const end = start + pagination.pageSize
    activityList.value = mockData.slice(start, end)
    pagination.total = mockData.length
  } catch (error) {
    MessagePlugin.error('获取活动列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  fetchActivityList()
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.status = ''
  handleSearch()
}

// 分页变化
const handlePageChange = (pageInfo) => {
  pagination.current = pageInfo.current
  pagination.pageSize = pageInfo.pageSize
  fetchActivityList()
}

// 编辑活动
const handleEdit = (row) => {
  MessagePlugin.info(`编辑活动: ${row.name}`)
}

// 删除活动
const handleDelete = (row) => {
  MessagePlugin.warning(`删除活动: ${row.name}`)
}

// 查看详情
const handleView = (row) => {
  MessagePlugin.info(`查看活动详情: ${row.name}`)
}

onMounted(() => {
  fetchActivityList()
})
</script>

<template>
  <div class="activity-list-container">
    <!-- 搜索区域 -->
    <t-card class="search-card">
      <t-form :data="searchForm" layout="inline">
        <t-form-item label="活动名称">
          <t-input v-model="searchForm.name" placeholder="请输入活动名称" clearable />
        </t-form-item>
        <t-form-item label="状态">
          <t-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <t-option value="pending" label="未开始" />
            <t-option value="ongoing" label="进行中" />
            <t-option value="ended" label="已结束" />
          </t-select>
        </t-form-item>
        <t-form-item>
          <t-space>
            <t-button theme="primary" @click="handleSearch">查询</t-button>
            <t-button theme="default" @click="handleReset">重置</t-button>
          </t-space>
        </t-form-item>
      </t-form>
    </t-card>

    <!-- 操作区域 -->
    <t-card class="table-card">
      <div class="table-header">
        <h3>活动列表</h3>
        <t-button theme="primary">
          <template #icon><t-icon name="add" /></template>
          新增活动
        </t-button>
      </div>

      <!-- 表格 -->
      <t-table
        :data="activityList"
        :columns="columns"
        :loading="loading"
        :pagination="pagination"
        @page-change="handlePageChange"
        stripe
        hover
      >
        <template #operation="{ row }">
          <t-space>
            <t-link theme="primary" hover="color" @click="handleView(row)">详情</t-link>
            <t-link theme="primary" hover="color" @click="handleEdit(row)">编辑</t-link>
            <t-link theme="danger" hover="color" @click="handleDelete(row)">删除</t-link>
          </t-space>
        </template>
      </t-table>
    </t-card>
  </div>
</template>

<style scoped>
.activity-list-container {
  height: 100%;
}

.search-card {
  margin-bottom: 16px;
}

.table-card {
  height: calc(100% - 100px);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.table-header h3 {
  margin: 0;
}
</style> 